Building Your Website Foundation Step-by-Step Guide to Installing LAMP Stack on CentOS 8
· 14 min read
Introduction
A "LAMP" stack is like a group of tools that work together to help your computer host websites and web apps made with PHP. Think of it as a team: "L" stands for Linux, the operating system; "A" is for Apache, the web server; "M" is for MariaDB, where all the important data hangs out; and "P" is for PHP, the tool that makes things happen on the website. Together, they create a combination to bring your websites to life!
In a LAMP stack, which helps your computer host websites, the part that manages data is usually a MySQL database server. However, for CentOS 8, the default toolbox didn't include MySQL. So, people started using MariaDB instead. Think of MariaDB as a friendly cousin of MySQL—it does the same job! The cool thing is, you can switch to MariaDB without needing to change anything in how your website works. It's like swapping out one tool for another without causing any trouble. Easy peasy!
In this guide, you're going to set up a special toolbox called a LAMP stack on a CentOS 8 server. We'll use MariaDB as the manager for all the important information your website needs. It's like creating a superhero team for your website! Let's get started!
What You Need Before Setting Up Your Website on CentOS 8
Before we start, make sure you have a special computer running CentOS 8. You should be logged in as a regular user with some extra powers (sudo privileges), and there's a protective shield on your computer called a firewall. If you haven't set up your computer this way yet, no worries! You can check out our beginner's guide called "Initial Server Setup Guide for CentOS 8" to get everything ready. Once you're set up, we can dive into the fun stuff!
Step 1: Setting Up the Apache Web Server
To show web pages to our visitors, we're going to use Apache, a widely used open-source web server that can be set up to display PHP pages. We'll use dnf, a tool that helps us install software on CentOS 8.
To install Apache, follow these steps:
Open the terminal on your CentOS 8 system. (This is like a command center where you can type instructions to your computer.) Type the following command and press Enter:
sudo dnf install httpd
When asked, type 'y' and press Enter to confirm that you want to install Apache.
Once the installation is done, use the following command to turn on and start the server:
sudo systemctl start httpd
If you've set up the firewalld firewall as we instructed earlier, you'll need to allow connections to Apache. Use the following command to permanently allow HTTP connections, which use port 80 by default:
sudo firewall-cmd --permanent --add-service=http
To make sure the change worked, you can check by running:
sudo firewall-cmd --permanent --list-all
You'll get a response that looks something like this:
Output
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: cockpit dhcpv6-client http ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
You'll have to reload the firewall settings so that the changes become active:
sudo firewall-cmd --reload
Now that you've added the new firewall rule, you can check if the server is working by opening your web browser and typing in your server's public IP address or domain name.
Note: If you're using Cloudtopiaa as your DNS hosting provider, you can find step-by-step instructions in our product documentation on how to set up a new domain name and connect it to your server.
If you haven't linked a domain name to your server yet and you're not sure of your server's public IP address, you can find it by running this command:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's//.*$//'
This will show you a few different IP addresses. You can try each of them one by one in your web browser.
Alternatively, you can check which IP address is reachable from other places on the internet:
curl -4 icanhazip.com
Copy and paste the IP address that you see into your web browser's address bar, and it will take you to Apache's default landing page.
If you can view this page, it means your web server is now installed correctly.
Step 2 — Adding MariaDB to the server
Now that your web server is set up, you need to install a database system to store and manage data for your website. We'll install MariaDB, which is similar to MySQL and is commonly used for web applications.
To install MariaDB, follow these steps,Run the following command:
sudo dnf install mariadb-server
Once the installation is complete, you can turn on and start the MariaDB server by:
sudo systemctl start mariadb
To enhance the security of your database server, it's a good idea to run a security script provided with MariaDB. This script helps by removing some insecure default settings and tightening access to your database system. To start the interactive script, simply run:
sudo mysql_secure_installation
This script will guide you through a series of questions where you can adjust your MariaDB setup. The first question will ask for the current database root password. This is different from the system root user. The database root user has complete control over the database system. Since you've just installed MariaDB and haven't set any password yet, simply press ENTER when prompted.
The next question asks if you want to create a password for the database root user. Since MariaDB uses a more secure authentication method for the root user by default, you don't need to set a password at this time. Simply type 'N' and then press ENTER.
After that, you can press 'Y' and then ENTER to accept the default options for all the following questions. This will remove anonymous users and the test database, disable remote root login, and apply these new rules immediately so the server will enforce the changes you made.
Once you're done, log in to the MariaDB console by typing:
sudo mysql
This command connects to the MariaDB server as the main database user called root. The use of "sudo" before the command helps to infer this. When you run this command, you'll likely see something similar to this output:
Output
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 9
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MariaDB [(none)]>
You may have noticed that you didn't have to enter a password to log in as the root user. This is because the default way MariaDB handles authentication for the administrative user is through unix_socket instead of using a password. This might seem like a security concern initially, but it actually makes the database server more secure. It ensures that only users with sudo privileges on the system can log in as the MariaDB root user, either directly from the command line or from applications running with similar privileges.In simpler terms, this means you won't be able to use the root user to connect to the database from a PHP application.
For better security, it's a good practice to have separate user accounts with limited privileges set up for each database, especially if you're hosting multiple databases on your server. To illustrate this setup, we'll create a database called "example_database" and a user named "example_user". Feel free to use different names if you prefer.
To create a new database, enter the following command in your MariaDB console:
CREATE DATABASE example_database;
Now you can make a new user and give them complete access to the database you just made. The following command sets the user's password as "password," but you should use a strong password of your own choice:
GRANT ALL ON example_database.* TO 'example_user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
This grants the user "example_user" full control over the "example_database" database, while ensuring they can't create or change other databases on your server.
After that, you need to flush the privileges to make sure they're saved and ready for use in the current session:
FLUSH PRIVILEGES;
After completing the previous steps, you can exit the MariaDB shell by typing:
exit
To check if the new user has the correct permissions, log in to the MariaDB console once more, but this time use the credentials of the custom user:
mysql -u example_user -p
In this command, notice the "-p" flag, which will ask you to enter the password you selected when creating the "example_user" user. Once you're logged in to the MariaDB console, check to see if you can access the "example_database" database:
SHOW DATABASES;
You'll see something like this displayed:
Output
+--------------------+
| Database |
+--------------------+
| example_database |
| information_schema |
+--------------------+
2 rows in set (0.000 sec)
To leave the MariaDB shell, simply type:
exit
Now that your database system is set up, you're ready to proceed with installing PHP, which is the last part of setting up the LAMP stack.
Step 3 — The PHP installation
Now that you have Apache set up for serving your website and MariaDB for managing your database, it's time to install PHP. PHP is what processes code to show dynamic content to your website visitors. Along with the main PHP package, we'll need php-mysqlnd, a module that lets PHP communicate with MySQL-based databases. Don't worry, the essential PHP packages will be automatically installed as dependencies.
To install PHP and php-mysqlnd packages using the dnf package manager, do the following:
sudo dnf install php php-mysqlnd
Once the installation is complete, you'll need to restart the Apache web server to activate the PHP module:
sudo systemctl restart httpd
Your web server is now ready to go! Next, we'll create a simple PHP testing script to ensure everything is working as it should.
Step 4 — Using Apache to test PHP
When you install Apache on CentOS 8, it automatically sets up a folder called "html" at /var/www/html. You don’t have to adjust any settings in Apache for PHP to function properly on your web server.
The only change we'll make is to adjust the permission settings on the folder where Apache stores its files. This will allow you to create and edit files in that folder without needing to use "sudo" before each command.
Use the following command to change the ownership of the default Apache document root to a user and group named "sammy". Make sure to replace "sammy" with your own username and group:
sudo chown -R sammy.sammy /var/www/html/
sudo chown -R sammy.sammy /var/www/html/
The default text editor in CentOS 8 is vi, which is powerful but may be challenging for beginners. If you prefer a simpler editor, you can install nano, which is more user-friendly, to help you edit files on your CentOS 8 server.
sudo dnf install nano
When asked, type 'y' to confirm the installation.
Next, let's create a new PHP file named "info.php" in the /var/www/html directory:
Next, let's create a new PHP file named "info.php" in the /var/www/html directory:
nano /var/www/html/info.php
Below is a simple PHP code that will show details about the PHP environment running on your server:
<?php
phpinfo();
When you're done, save and close the file. If you're using nano, you can do this by pressing CTRL+X, then Y, and finally hitting ENTER to confirm.
Now, let's check if our web server can correctly show content generated by a PHP script. Open your web browser and enter your server's hostname or IP address, followed by "/info.php":
http://server_host_or_IP/info.php
When you open the browser and visit your server's address followed by "/info.php", you'll see a page that looks something like this:
After you've finished checking the PHP server information on that page, it's a good idea to delete the file you created. This is because it contains sensitive details about your PHP environment and CentOS server. You can use the "rm" command to remove the file:
rm /var/www/html/info.php
If you ever need it again in the future, you can always create this file again. Now, let's move on to testing the database connection from the PHP side.
Step 5 — Connecting to the database from PHP (optional)
If you'd like to check whether PHP can connect to MariaDB and run database queries, you can create a test table with some sample data and then retrieve this data from a PHP script.
To begin, let's connect to the MariaDB console using the database user you created earlier in Step 2 of this guide:
mysql -u example_user -p
Let's create a table called "todo_list". In the MariaDB console, enter the following command:
CREATE TABLE example_database.todo_list (
item_id INT AUTO_INCREMENT,
content VARCHAR(255),
PRIMARY KEY(item_id)
);
Now, let's add some data into the "todo_list" table. You can repeat the following command a few times, each time using different values:
INSERT INTO example_database.todo_list (content) VALUES ("My first important item");
To make sure the data was saved correctly in your table, execute the following command:
SELECT * FROM example_database.todo_list;
You'll receive output similar to this:
Output
+---------+--------------------------+
| item_id | content |
+---------+--------------------------+
| 1 | My first important item |
| 2 | My second important item |
| 3 | My third important item |
| 4 | and this one more thing |
+---------+--------------------------+
4 rows in set (0.000 sec)
Once you've confirmed that you have valid data in your test table, you can exit the MariaDB console:
exit
Now, let's make the PHP script that will connect to MariaDB and retrieve your data. Create a new PHP file in your chosen web directory using your preferred editor. We'll use nano for this:
nano /var/www/html/todo_list.php
Copy and paste the following code into your PHP script:
<?php
$user = "example_user";
$password = "password";
$database = "example_database";
$table = "todo_list";
try {
$db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
echo "<h2>TODO</h2><ol>";
foreach($db->query("SELECT content FROM $table") as $row) {
echo "<li>" . $row['content'] . "</li>";
}
echo "</ol>";
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
Once you've finished editing, save and close the file.
Now, you can view this page in your web browser by entering your server's hostname or public IP address, followed by "/todo_list.php":
http://server_host_or_IP/todo_list.php
You'll see a page similar to this, displaying the content you added to your test table:
This indicates that your PHP setup is successfully connecting and communicating with your MariaDB server.
Conclusion:
In this guide, you've established a solid base for hosting PHP websites and applications for your visitors. You've configured Apache to handle PHP requests and created a MariaDB database to store your website's data.
Ready to launch your PHP-powered website? Let's get started!
If you have any questions or need further assistance, feel free to ask.